/**
 * @file SelectLayerMode.cpp
 *
 * @date 29/12/2011
 * @author Tales 
 *
 */

#include "SelectLayerMode.hpp"
#include <climits>
#include <iosfwd>
#include "../controller/Editor.hpp"

namespace yep
{

static const int STATE_PRESENTATION = 0, STATE_ASK = 1, STATE_END = -1;

pair<string,SelectLayerMode::Function> SelectLayerMode::getNextLine(Editor & editor)
{
	string text;
	Function function;
	switch (state)
	{
	case STATE_PRESENTATION:
	{
		stringstream s;
		s << "Camada atualmente selecionada: " << (editor.getCurrentLayer()+1) << "/"<< editor.getLayerCount() << ".";
		text = s.str();
		function = NO_FUNCTION;
		state = STATE_ASK;
		break;
	}
	case STATE_ASK:
	{
		stringstream s;
		s << "Qual camada deseja selecionar? (1 a " << editor.getLayerCount() << " = mudar; 0 = nao mudar): ";
		function = INPUT_INT;
		text = s.str();
		break;
	}
	case STATE_END:
		text = "";
		function = END;
		break;
	}

	return make_pair(text, function);
}



void SelectLayerMode::handleAnswer(Editor & editor, string answer)
{
	stringstream s(answer);
	size_t layerNumber;
	s >> layerNumber;
	if (layerNumber != 0)
	{
		--layerNumber; // 0 do usuário significa "não alterar". Mas pro editor 0 significa "LAyer #1", então excluímos o zero.
		if (layerNumber >= editor.getLayerCount()) // Se Layer inválida? então Repita a pergunta
			return;
		editor.setTool(layerNumber);
	}
	state = STATE_END;
}



void SelectLayerMode::setup()
{
	ConsoleModeBase::setup();
	state = STATE_PRESENTATION;
}

}

 /* namespace yep */
